package com.viaje.action.ranking;

import javax.persistence.*;

import org.apache.struts2.convention.annotation.*;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.validator.annotations.EmailValidator;
import com.opensymphony.xwork2.validator.annotations.RequiredStringValidator;
import com.opensymphony.xwork2.validator.annotations.StringLengthFieldValidator;
import com.opensymphony.xwork2.validator.annotations.Validations;
import com.opensymphony.xwork2.validator.annotations.ValidatorType;
import com.viaje.common.util.Utilitario;
import com.viaje.model.Ranking;


public class EditRankingAction extends ActionSupport {

	private Ranking ranking;
	private long id;
	private String message;
	@Action(value = "/edit-ranking", 
			params={"id","{1}"},
			results = {
			@Result(name = "error", type="tiles", location = "edit-ranking.tiles"),
			@Result(name = "exito", type="tiles", location = "message.tiles"),
			@Result(name = "input", type="tiles", location = "edit-ranking.tiles") },
		interceptorRefs={
			 @InterceptorRef("fileUpload"), 
			 @InterceptorRef("basicStack"),
			 @InterceptorRef("jsonValidationWorkflowStack")})
	@Validations(
		       requiredStrings =
		        	{  @RequiredStringValidator(type = ValidatorType.SIMPLE, fieldName = "ranking.nombre", key = "edit.ranking.nombre.required"),
		    		   @RequiredStringValidator(type = ValidatorType.SIMPLE, fieldName = "ranking.puesto", key = "edit.ranking.puesto.required"),  		   
		        	})	
	public String execute(){
		String result="exito";
		EntityManager em=
				Utilitario.getInstance().getEntityManager();
		ranking=em.find(Ranking.class, id);
		if(ranking==null){
			message="Ranking does not exists";
			result="error";
		}  
		return result;
	}

	@Action(value="/update-ranking",
			results={
					@Result(name="exito",type="tiles",location="message.tiles")},
			interceptorRefs={
			 		@InterceptorRef("fileUpload"), 
			 		@InterceptorRef("basicStack"),
			 		@InterceptorRef("jsonValidationWorkflowStack")}
		)

	public String update(){
		String result="exito";
		EntityManager em=
				Utilitario.getInstance().getEntityManager();
		EntityTransaction tx=em.getTransaction();
		Ranking rankingUpdate=em.find(Ranking.class, ranking.getId());
			try{
				tx.begin();
				rankingUpdate.setNombre(ranking.getNombre());
				rankingUpdate.setPuesto(ranking.getPuesto());
				em.merge(rankingUpdate);
				message = getText("edit.ranking.message.exito");
				tx.commit();
			}catch(Exception ex){
				ex.printStackTrace();
				tx.rollback();
				message = getText("edit.ranking.message.error");
			}
		return result;
	}
	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public Ranking getRanking() {
		return ranking;
	}

	public void setRanking(Ranking ranking) {
		this.ranking = ranking;
	}


		
}//FIN CLASS
